package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.DealHistoryDetailListAdapter;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.DealHistoryDetailListObjectResponce;
import com.iexbuy.ihg.entity.DealHistoryDetailObject;
import com.iexbuy.ihg.entity.UserProfile;
import com.iexbuy.ihg.entity.YunItemListObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeDeProfileFragment extends BaseFragment {

	private static WeDeProfileFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.userName)
	private EditText userName;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_profile, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.we_de_profile));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);
		mBaseActivity.setRightButton("保存", new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				saveNewName();
			}
		});

		userName.setText(App.sPreferences.getName());
	}

	private void saveNewName() {
		if (StringUtil.isEmpty(userName.getText().toString())) {
			ToastUtils.toast(mBaseActivity, "请输入用户姓名");
		}
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("userId", App.sPreferences.getUserId());
		paramMap.put("userName", userName.getText().toString());
		ServiceHelper.send(mBaseActivity, ServiceHelper.SUBMIT_USER_NAME, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						ToastUtils.toast(mBaseActivity, "保存用户名成功！");
						App.sPreferences.setName(userName.getText().toString());
						mBaseActivity.finish();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeProfileFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
